package lxt.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import lxt.action.core.ActionSupport;
import lxt.action.core.DateUtil;
import lxt.entity.CostPetrol;
import lxt.entity.DriverInfo;
import lxt.entity.InvoiceOrder;
import lxt.entity.Parameter;
import lxt.entity.TruckInfo;
import lxt.entity.UserInfo;
import lxt.service.CostPetrolService;
import lxt.service.DriverService;
import lxt.service.InvoiceOrderService;
import lxt.service.ParameterService;
import lxt.service.TruckService;
/**
 * 加油管理action
 * @author Lit
 *
 */
public class CostPetrolAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private String id;
    private String truckId;
    private String orderId;
    private String driverId;
    private String petrolType;
    private String petrolQuantity;
    private String unitPrice;
    private String petrolStation;
    private String totalCharge;
    private String petrolDate;
    private String note;
    private Date entryTime;
    private String entryUser;
	//定义service
    private CostPetrolService costPetrolService;
    private DriverService driverService ; //用户获取驾驶员
    private TruckService truckService ; //用于获取车辆
    private InvoiceOrderService invoiceOrderService ;//用于获取订单号
    private ParameterService parameterService ;
    //定义对象
	private List<CostPetrol> costPetrols;
	private List<TruckInfo> trucks; //车牌号list
	private List<InvoiceOrder> invoiceOrders ; //订单号list
	private List<DriverInfo> drivers ; //驾驶员list
	private List<Parameter> paramter ;
	
	private String deleteIds;
	//定义分页变量
	private Integer pageCounts = 10; //每页显示数量
	private String currPageStr = "1"; //分页
	private Integer totalCounts ;//总数据量
    
	/**
	 * 加载
	 * @return
	 */
	public String loadAll(){
		try{
			//定义从第几条显示
			int offset =( Integer.parseInt(currPageStr)-1)*pageCounts;
			//获得交通事故列表
			costPetrols = costPetrolService.loadCostPetrol(truckId, driverId, offset, pageCounts);
			//获取总的记录数
			totalCounts = costPetrolService.loadAllCostPetrolCount(truckId, driverId);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加预处理
	 * @return
	 */
	public String AddPre(){
		try{
			trucks = truckService.loadAll();
			drivers = driverService.loadAll();
			invoiceOrders = invoiceOrderService.loadAll();
			paramter = parameterService.loadByParamType("petrolType");
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加
	 * @return
	 */
	public String add(){
		try{
			//创建新的对象
			CostPetrol costPetrol = new CostPetrol();
			//为对象赋值
			costPetrol.setDriverId(driverId);
			UserInfo user = (UserInfo)this.getSession().get("user");
			costPetrol.setEntryUser(user.getUsername());
			costPetrol.setNote(note);
			costPetrol.setOrderId(orderId);
			if(petrolDate!=null&&!petrolDate.equals("")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				costPetrol.setPetrolDate(sdf.parse(petrolDate));
			}
			if(petrolQuantity!=null&&!petrolQuantity.equals("")){
				costPetrol.setPetrolQuantity(Double.parseDouble(petrolQuantity));
			}
			costPetrol.setPetrolStation(petrolStation);
			if(petrolType!=null&&!petrolType.equals("")){
				costPetrol.setPetrolType(petrolType);
			}
			if(totalCharge!=null&&!totalCharge.equals("")){
				costPetrol.setTotalCharge(Double.parseDouble(totalCharge));
			}
			if(unitPrice!=null&&!unitPrice.equals("")){
				costPetrol.setUnitPrice(Double.parseDouble(unitPrice));
			}
			costPetrol.setTruckId(truckId);
			this.costPetrolService.addCostPetrol(costPetrol);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 编辑预处理
	 * @return
	 */
	public String updatePre(){
		try{
			trucks = truckService.loadAll();
			drivers = driverService.loadAll();
			invoiceOrders = invoiceOrderService.loadAll();
			paramter = parameterService.loadByParamType("petrolType");
			//获得参数
			CostPetrol costPetrol = this.costPetrolService.loadCostPetrolById(id);
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				Date preTime = costPetrol.getEntryTime();
				int falg = DateUtil.getDays(preTime);
				if(falg!=0){
					this.addActionMessage("无权修改数据,请联系管理员");
					this.loadAll();
					return INPUT;
				}
			}
			//为页面属性赋值
			this.driverId = costPetrol.getDriverId();
			this.id = costPetrol.getId();
			this.note = costPetrol.getNote();
			this.orderId = costPetrol.getOrderId();
			this.petrolDate = costPetrol.getPetrolDate().toString();
			this.petrolQuantity = String.valueOf(costPetrol.getPetrolQuantity());
			this.petrolStation = costPetrol.getPetrolStation();
			this.petrolType = String.valueOf(costPetrol.getPetrolType());
			this.totalCharge = String.valueOf(costPetrol.getTotalCharge());
			this.truckId = costPetrol.getTruckId();
			this.unitPrice = String.valueOf(costPetrol.getUnitPrice());
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 修改
	 * @return
	 */
	public String update(){
		try{
			//重新获取修改目标
			CostPetrol costPetrol = this.costPetrolService.loadCostPetrolById(id);
			//更新目标对象的目标修改属性(部分属性)
			costPetrol.setDriverId(driverId);
			UserInfo user = (UserInfo)this.getSession().get("user");
			costPetrol.setEntryUser(user.getUsername());
			costPetrol.setNote(note);
			costPetrol.setOrderId(orderId);
			if(petrolDate!=null&&!petrolDate.equals("")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				costPetrol.setPetrolDate(sdf.parse(petrolDate));
			}
			if(petrolQuantity!=null&&!petrolQuantity.equals("")){
				costPetrol.setPetrolQuantity(Integer.parseInt(petrolQuantity));
			}
			costPetrol.setPetrolStation(petrolStation);
			if(petrolType!=null&&!petrolType.equals("")){
				costPetrol.setPetrolType(petrolType);
			}
			if(totalCharge!=null&&!totalCharge.equals("")){
				costPetrol.setTotalCharge(Double.parseDouble(totalCharge));
			}
			if(unitPrice!=null&&!unitPrice.equals("")){
				costPetrol.setUnitPrice(Double.parseDouble(unitPrice));
			}
			costPetrol.setTruckId(truckId);
			this.costPetrolService.updateCostPetrol(costPetrol);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		try{
			/**
			 * 查看用户操作时间 设置编辑权限
			 */
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				for(int i=0;i<deleteIds.length();i++){
					String[] delId = deleteIds.split(",");
					CostPetrol c = this.costPetrolService.loadCostPetrolById(delId[i]);
					int falg = DateUtil.getDays(c.getEntryTime());
					if(falg!=0){
						this.addActionMessage("无权修改数据,请联系管理员");
						this.loadAll();
						return INPUT;
					}
				}
			}
			this.costPetrolService.deleteCostPetrol(deleteIds);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	
	/**
	 * get set
	 * @return
	 */
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTruckId() {
		return truckId;
	}
	public void setTruckId(String truckId) {
		this.truckId = truckId;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getDriverId() {
		return driverId;
	}
	public void setDriverId(String driverId) {
		this.driverId = driverId;
	}
	public String getPetrolType() {
		return petrolType;
	}
	public void setPetrolType(String petrolType) {
		this.petrolType = petrolType;
	}
	public String getPetrolQuantity() {
		return petrolQuantity;
	}
	public void setPetrolQuantity(String petrolQuantity) {
		this.petrolQuantity = petrolQuantity;
	}
	public String getUnitPrice() {
		return unitPrice;
	}
	public void setUnitPrice(String unitPrice) {
		this.unitPrice = unitPrice;
	}
	public String getPetrolStation() {
		return petrolStation;
	}
	public void setPetrolStation(String petrolStation) {
		this.petrolStation = petrolStation;
	}
	public String getTotalCharge() {
		return totalCharge;
	}
	public void setTotalCharge(String totalCharge) {
		this.totalCharge = totalCharge;
	}
	public String getPetrolDate() {
		return petrolDate;
	}
	public void setPetrolDate(String petrolDate) {
		this.petrolDate = petrolDate;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public Date getEntryTime() {
		return entryTime;
	}
	public void setEntryTime(Date entryTime) {
		this.entryTime = entryTime;
	}
	public String getEntryUser() {
		return entryUser;
	}
	public void setEntryUser(String entryUser) {
		this.entryUser = entryUser;
	}
	public CostPetrolService getCostPetrolService() {
		return costPetrolService;
	}
	public void setCostPetrolService(CostPetrolService costPetrolService) {
		this.costPetrolService = costPetrolService;
	}
	public DriverService getDriverService() {
		return driverService;
	}
	public void setDriverService(DriverService driverService) {
		this.driverService = driverService;
	}
	public TruckService getTruckService() {
		return truckService;
	}
	public void setTruckService(TruckService truckService) {
		this.truckService = truckService;
	}
	public InvoiceOrderService getInvoiceOrderService() {
		return invoiceOrderService;
	}
	public void setInvoiceOrderService(InvoiceOrderService invoiceOrderService) {
		this.invoiceOrderService = invoiceOrderService;
	}
	public List<CostPetrol> getCostPetrols() {
		return costPetrols;
	}
	public void setCostPetrols(List<CostPetrol> costPetrols) {
		this.costPetrols = costPetrols;
	}
	public List<TruckInfo> getTrucks() {
		return trucks;
	}
	public void setTrucks(List<TruckInfo> trucks) {
		this.trucks = trucks;
	}
	public List<InvoiceOrder> getInvoiceOrders() {
		return invoiceOrders;
	}
	public void setInvoiceOrders(List<InvoiceOrder> invoiceOrders) {
		this.invoiceOrders = invoiceOrders;
	}
	public List<DriverInfo> getDrivers() {
		return drivers;
	}
	public void setDrivers(List<DriverInfo> drivers) {
		this.drivers = drivers;
	}
	public String getDeleteIds() {
		return deleteIds;
	}
	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}
	public Integer getPageCounts() {
		return pageCounts;
	}
	public void setPageCounts(Integer pageCounts) {
		this.pageCounts = pageCounts;
	}
	public String getCurrPageStr() {
		return currPageStr;
	}
	public void setCurrPageStr(String currPageStr) {
		this.currPageStr = currPageStr;
	}
	public Integer getTotalCounts() {
		return totalCounts;
	}
	public void setTotalCounts(Integer totalCounts) {
		this.totalCounts = totalCounts;
	}
	public ParameterService getParameterService() {
		return parameterService;
	}
	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}
	public List<Parameter> getParamter() {
		return paramter;
	}
	public void setParamter(List<Parameter> paramter) {
		this.paramter = paramter;
	}

}
